package com.xiaoshuidi.cloud.module.rooms.controller.app.home;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.tenant.core.context.TenantContextHolder;
import com.xiaoshuidi.cloud.module.rooms.controller.app.home.vo.*;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.service.HomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import java.util.List;

/**
 * 租客端-首页相关接口
 *
 * @author hans.zhang
 */

@Tag(name = "租客端 - 首页相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiConstants.NAME + "/home")
public class HomeController {

    private final HomeService homeService;

    @Operation(summary = "集中式-搜索找房")
    @Parameters({
            @Parameter(name = "searchBox", description = "搜索框查询"),
            @Parameter(name = "district", description = "区域"),
            @Parameter(name = "monthRentalMin", description = "租金范围-下限"),
            @Parameter(name = "monthRentalMax", description = "租金范围-上限"),
            @Parameter(name = "longitude", description = "地铁站经度"),
            @Parameter(name = "latitude", description = "地铁站纬度"),
            @Parameter(name = "sortRule", description = "排序规则 1:推荐排序 2:最新发布 3:价格升序 4:价格降序 5:面积升序 6:面积降序"),
            @Parameter(name = "isWhole", description = "出租方式,是否整租 true:整租 false:合租"),
            @Parameter(name = "bedRoomNum", description = "户型-几室 1:一室 2:二室 3:三室 4:四室 5:其他"),
            @Parameter(name = "publicFacilities", description = "特色需求(公共设施)", example = "独立卫生间"),
            @Parameter(name = "lon", description = "用户所在经度"),
            @Parameter(name = "lat", description = "用户所在纬度"),
    })
    @PermitAll
    @GetMapping("/concentrated-find-house")
    public CommonResult<PageResult<ConcentratedFindHouseRespVO>> concentratedFindHouse(String searchBox,
                                                                                       String district,
                                                                                       Double monthRentalMin,
                                                                                       Double monthRentalMax,
                                                                                       Double longitude,
                                                                                       Double latitude,
                                                                                       Integer sortRule,
                                                                                       Boolean isWhole,
                                                                                       Integer bedRoomNum,
                                                                                       String publicFacilities,
                                                                                       String lon,
                                                                                       String lat,
                                                                                       Integer pageNum,
                                                                                       Integer pageSize) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.concentratedFindHouse(searchBox, district, monthRentalMin, monthRentalMax, longitude, latitude, sortRule, isWhole, bedRoomNum, publicFacilities, pageNum, pageSize, tenantId, lon, lat);
    }

    @Operation(summary = "分散式-搜索找房")
    @Parameters({
            @Parameter(name = "searchBox", description = "搜索框查询"),
            @Parameter(name = "district", description = "区域"),
            @Parameter(name = "monthRentalMin", description = "租金范围-下限"),
            @Parameter(name = "monthRentalMax", description = "租金范围-上限"),
            @Parameter(name = "longitude", description = "地铁站经度"),
            @Parameter(name = "latitude", description = "地铁站纬度"),
            @Parameter(name = "sortRule", description = "排序规则 1:推荐排序 2:最新发布 3:价格升序 4:价格降序 5:面积升序 6:面积降序"),
            @Parameter(name = "isWhole", description = "出租方式,是否整租 true:整租 false:合租"),
            @Parameter(name = "bedRoomNum", description = "户型-几室 1:一室 2:二室 3:三室 4:四室 5:其他"),
            @Parameter(name = "publicFacilities", description = "特色需求(公共设施)", example = "独立卫生间"),
    })
    @GetMapping("/no-concentrated-find-house")
    @PermitAll
    public CommonResult<PageResult<NoConcentratedFindHouseRespVO>> noConcentratedFindHouse(String searchBox,
                                                                                           String district,
                                                                                           Double monthRentalMin,
                                                                                           Double monthRentalMax,
                                                                                           Double longitude,
                                                                                           Double latitude,
                                                                                           Integer sortRule,
                                                                                           Boolean isWhole,
                                                                                           Integer bedRoomNum,
                                                                                           String publicFacilities,
                                                                                           Long roomAreaId,
                                                                                           Integer pageNum,
                                                                                           Integer pageSize) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.noConcentratedFindHouse(searchBox, district, monthRentalMin, monthRentalMax, longitude, latitude, sortRule, isWhole, bedRoomNum, publicFacilities, pageNum, pageSize, tenantId, roomAreaId);
    }

    @Operation(summary = "社区-搜索房源")
    @Parameters({
            @Parameter(name = "isConcentrated", description = "是否集中式 true:集中式 false:分散式 null:全部"),
            @Parameter(name = "searchBox", description = "搜索框内容"),
    })
    @GetMapping("/search-room")
    @PermitAll
    public CommonResult<PageResult<SearchRoomRespVo>> searchRoom(Boolean isConcentrated, String searchBox, Integer pageNo, Integer pageSize) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.searchRoom(isConcentrated, searchBox, pageNo, pageSize, tenantId);
    }

    @Operation(summary = "社区-门店详情")
    @Parameter(name = "apartmentId", description = "门店id")
    @GetMapping("/get-apartment-info")
    @PermitAll
    public CommonResult<GetApartmentInfoRespVO> getApartmentInfo(@RequestParam("apartmentId") Long apartmentId) {
        return homeService.getApartmentInfo(apartmentId);
    }

    @Operation(summary = "社区-房型详情")
    @Parameters({
            @Parameter(name = "apartmentId", description = "门店id"),
            @Parameter(name = "layoutId", description = "房型id")
    })
    @GetMapping("/get-layout-info")
    @PermitAll
    public CommonResult<GetLayoutInfoRespVO> getLayoutInfo(@RequestParam("apartmentId") Long apartmentId, @RequestParam("layoutId") Long layoutId) {
        return homeService.getLayoutInfo(apartmentId, layoutId);
    }

    @Operation(summary = "社区-房源详情")
    @Parameters({
            @Parameter(name = "roomHouseId", description = "房源id"),
    })
    @GetMapping("/get-room-house-info")
    @PermitAll
    public CommonResult<GetRoomHouseInfoRespVO> getRoomHouseInfo(@RequestParam("roomHouseId") Long roomHouseId) {
        return homeService.getRoomHouseInfo(roomHouseId);
    }

    @Operation(summary = "社区-房源详情-同小区房源")
    @GetMapping("/same-residential-listings")
    @PermitAll
    public CommonResult<PageResult<SameResidentialListingsRespVO>> sameResidentialListings(SameResidentialListingsReqVO reqVO) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.sameResidentialListings(reqVO.getRoomHouseId(), reqVO.getPageNo(), reqVO.getPageSize(), tenantId);
    }

    @Operation(summary = "社区-房源详情-同房源其他房间")
    @Parameters({
            @Parameter(name = "roomHouseId", description = "房源id"),
    })
    @GetMapping("/other-rooms-in-the-same-listing")
    @PermitAll
    public CommonResult<List<OtherRoomsInTheSameListingRespVO>> otherRoomsInTheSameListing(@RequestParam("roomHouseId") Long roomHouseId) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.otherRoomsInTheSameListing(roomHouseId, tenantId);
    }

    @Operation(summary = "社区-房源详情-推荐附近房源")
    @Parameters({
            @Parameter(name = "roomHouseId", description = "房源id"),
    })
    @GetMapping("/recommend-listings-near-you")
    @PermitAll
    public CommonResult<List<OtherRoomsInTheSameListingRespVO>> recommendListingsNearYou(@RequestParam("roomHouseId") Long roomHouseId) {
        Long tenantId = TenantContextHolder.getTenantId();
        return homeService.recommendListingsNearYou(roomHouseId, tenantId);
    }
}
